package com.changdu.seatunnel.admin.mapper.sql;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;

public abstract class RepositoryDataSource {

	/**
	 * 初始化数据源
	 */
	private DataSource dataSource;
	
	public RepositoryDataSource(DataSourceProperties dataSourceProperties) {
		DataSourceBuilder<?> builder = DataSourceBuilder.create();
		builder.driverClassName(dataSourceProperties.getDriverClassName());
		builder.url(dataSourceProperties.getUrl());
		builder.username(dataSourceProperties.getUsername());
		builder.password(dataSourceProperties.getPassword());
		this.dataSource = builder.build();
	}
	
	/**
	 * 获取数据源
	 */
	protected DataSource dataSource() {
		return dataSource;
	}
	
	/**
	 * 获取连接
	 * @return
	 * @throws SQLException
	 */
	protected Connection getConnect() throws SQLException {
		return dataSource.getConnection();
	}

}
